package mensaplan;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
import de.app.badresden.AnimationTouchListener;
import de.app.badresden.IDatePickerActivity;
import de.app.badresden.R;

public class MensaActivity extends Activity implements OnItemClickListener, OnClickListener,
		IDatePickerActivity, OnCancelListener {

	private ProgressDialog progressDialog;
	private MensaplanBuilder mensaplanbuilder;
	private Calendar cal = Calendar.getInstance();
	private Button datePickerButton;
	private ViewFlipper viewflipper;
	private LinearLayout flipperone;
	private LinearLayout flippertwo;
	private boolean flipperOneActive = true;

	public void onCreate(Bundle icicle) {

		super.onCreate(icicle);

		progressDialog = ProgressDialog.show(MensaActivity.this,
				getString(R.string.progressdialog_title),
				getString(R.string.progressdialog_mensaplan), true, true, this);

		Thread importMahlzeitenThread = new Thread() {

			public void run() {

				try {
					synchronized (this) {
						List<MensaMahlzeit> mahlzeiten = new MensaMahlzeitenImport()
								.ImportMensa(getApplicationContext());

						Bundle bundle = new Bundle();
						bundle.putSerializable("mahlzeiten", (Serializable) mahlzeiten);
						Message msg = new Message();
						msg.setData(bundle);
						updateHandler.sendMessage(msg);
					}
				}
				catch (Exception e) {
					Log.d("badresden app", e.toString());
				}
			}
		};
		importMahlzeitenThread.start();
	}

	private Handler updateHandler = new Handler() {

		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {

			super.handleMessage(msg);

			Bundle bundle = msg.getData();
			List<MensaMahlzeit> mahlzeiten = (ArrayList<MensaMahlzeit>) bundle
					.getSerializable("mahlzeiten");

			setContentView(R.layout.mensaplanlayout);

			datePickerButton = (Button) findViewById(R.id.mensaplanlayout_datePickerBtn);
			viewflipper = (ViewFlipper) findViewById(R.id.mensaplan_layoutswitcher);
			flipperone = (LinearLayout) findViewById(R.id.mensaplan_flipperone);
			flippertwo = (LinearLayout) findViewById(R.id.mensaplan_flippertwo);

			AnimationTouchListener touchListener = new AnimationTouchListener(
					MensaActivity.this, viewflipper);
			datePickerButton.setOnClickListener(MensaActivity.this);
			viewflipper.setOnTouchListener(touchListener);

			mensaplanbuilder = new MensaplanBuilder(MensaActivity.this, touchListener);
			mensaplanbuilder.ImportMahlzeiten(mahlzeiten);
			mensaplanbuilder.updateMensaplan(cal, flipperone);

			updateDatePickerButton();
			progressDialog.dismiss();
		}
	};

	@Override
	public void onClick(View clickedButton) {

		if (clickedButton == datePickerButton) {
			showDialog(0);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> l, View v, int position, long id) {

		MensaMahlzeit mahlzeit = (MensaMahlzeit) l.getAdapter().getItem(position);

		boolean noinfo = !mahlzeit.getBeschreibung().equals(getString(R.string.mensa_noinfo));
		boolean keinangebot = !mahlzeit.getBeschreibung().contains("Kein Angebot");
		
		if (noinfo && keinangebot) {

			Intent intent = new Intent();
			Bundle bun = new Bundle();

			bun.putSerializable("MensaMahlzeit", (Serializable) mahlzeit);

			intent.setClass(this, MensaMahlzeitDetailsActivity.class);
			intent.putExtras(bun);
			startActivity(intent);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		return new DatePickerDialog(this, DatePickerListener, cal.get(Calendar.YEAR),
				cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
	}

	private DatePickerDialog.OnDateSetListener DatePickerListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

			cal.set(year, monthOfYear, dayOfMonth);
			updateDatePickerButton();
			updateActiveView();
		}
	};

	@Override
	public void updateDatePickerButton() {

		Locale loc = getBaseContext().getResources().getConfiguration().locale;
		SimpleDateFormat dayformat = new SimpleDateFormat("EEEE", loc);
		SimpleDateFormat monthformat = new SimpleDateFormat("MMM", loc);

		String dateText = dayformat.format(cal.getTime()) + ", "
				+ String.valueOf(cal.get(Calendar.DAY_OF_MONTH)) + ". "
				+ monthformat.format(cal.getTime()) + " "
				+ String.valueOf(cal.get(Calendar.YEAR));

		datePickerButton.setText(dateText);
	}

	@Override
	public void addDay() {

		cal.add(Calendar.DAY_OF_MONTH, 1);
	}

	@Override
	public void subDay() {

		cal.add(Calendar.DAY_OF_MONTH, -1);
	}

	@Override
	public void updateNextView() {

		if (flipperOneActive) {
			mensaplanbuilder.updateMensaplan(cal, flippertwo);
			flipperOneActive = false;
		}
		else {
			mensaplanbuilder.updateMensaplan(cal, flipperone);
			flipperOneActive = true;
		}
	}

	@Override
	public void updateActiveView() {

		if (flipperOneActive) mensaplanbuilder.updateMensaplan(cal, flipperone);
		else
			mensaplanbuilder.updateMensaplan(cal, flippertwo);
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		progressDialog.dismiss();
		finish();
	}
}